/**
 * @date 2005-12-21 13:17:17
 *
 */
package org.nanhill.xsheet.xdata.output.xml;

import static org.nanhill.xsheet.xdata.XDataConstants.INDEX;
import static org.nanhill.xsheet.xdata.XDataConstants.ROW;
import static org.nanhill.xsheet.xdata.XDataConstants.XDATA;
import static org.nanhill.xsheet.xdata.XDataConstants.XINDEX;
import static org.nanhill.xsheet.xdata.XDataConstants.XSET;

import org.dom4j.DocumentHelper;
import org.dom4j.Element;

/**
 * @author alin
 * 
 */
public class XDataDefaultXmlOutputBuilder extends XDataXmlOutputBuilder {
/*	private static final Log logger = LogFactory
			.getLog(XDataDefaultXmlOutputBuilder.class);*/

	private void buildEachRow(Element ele, Object[] row) {
		for (int i = 0; i < row.length;) {
			Object name = row[i];
			Object value = row[i + 1];
			ele.addAttribute(name.toString(), value.toString());
			i += 2;
		}
	}

	private void buildDocument() {
		this.document = DocumentHelper.createDocument();
		Element root = document.addElement(XDATA);
		Element ele = root.addElement(XINDEX);
		Element rowElement;
		for (Object[] row : this.model.getIndexs()) {
			rowElement = ele.addElement(INDEX);
			this.buildEachRow(rowElement, row);
		}
		ele = root.addElement(XSET);
		for (Object[] row : this.model.getSets()) {
			rowElement = ele.addElement(ROW);
			this.buildEachRow(rowElement, row);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.nanhill.base.pattern.Director#construct()
	 */
	public void construct() throws Exception {
		this.buildDocument();
	}

}
